package com.qing.design.patterns.observer.example.publisher;

import com.qing.design.patterns.observer.example.listeners.EventListener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事件管理器
 */
public class EventManager {

    Map<String, List<EventListener>> listeners=new HashMap<>();

    public EventManager(String... operations) {
        for (String operation : operations) {
            this.listeners.put(operation,new ArrayList<>());
        }
    }

    public void subscribe(String eventType,EventListener eventListener){
        List<EventListener> users=listeners.get(eventType);
        users.add(eventListener);
    }

    public void unsubscribe(String eventType,EventListener eventListener){
        List<EventListener> users=listeners.get(eventType);
        users.remove(eventListener);
    }

    /**
     * 通知
     * @param eventType
     * @param file
     */
    public void notify(String eventType, File file) {
        List<EventListener> users = listeners.get(eventType);
        for (EventListener user : users) {
            user.update(eventType, file);
        }
    }
}
